package org.jeecg.modules.coucoursearrange.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.config.mybatis.TenantContext;
import org.jeecg.modules.classes.entity.CouClass;
import org.jeecg.modules.classes.service.ICouClassService;
import org.jeecg.modules.common.api.ISysInfo;
import org.jeecg.modules.common.baseentity.BaseApiEntity;
import org.jeecg.modules.common.utils.BaseTransfer;
import org.jeecg.modules.common.utils.BeanObjUtil;
import org.jeecg.modules.common.utils.MapBeanUtils;
import org.jeecg.modules.coucoursearrange.entity.CouCourseArrange;
import org.jeecg.modules.coucoursearrange.service.ICouCourseArrangeService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.courschetemplateatom.entity.CouCourScheTemplateAtom;
import org.jeecg.modules.courschetemplateatom.service.ICouCourScheTemplateAtomService;
import org.jeecg.modules.coursearrangeatom.entity.CouCourseArrangeAtom;
import org.jeecg.modules.coursearrangeatom.service.ICouCourseArrangeAtomService;
import org.jeecg.modules.couteachingjobexecute.service.ICouTeachingJobExecuteService;
import org.jeecg.modules.couteachingjobexecute.vo.CouTeachingJobExecuteVo;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.student.vo.StuStudentVo;
import org.jeecg.modules.teacher.entity.TeaTeacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: cou_course_arrange
 * @author: baochangrong
 * @Date: 2022-02-25
 * @Version: V1.0
 */
@Api(tags = "cou_course_arrange")
@RestController
@RequestMapping("/coucoursearrange/couCourseArrange")
@Slf4j
public class CouCourseArrangeController extends JeecgController<CouCourseArrange, ICouCourseArrangeService> {
    @Autowired
    private ICouCourseArrangeService couCourseArrangeService;
    @Autowired
    private ICouCourseArrangeAtomService couCourseArrangeAtomService;
    @Autowired
    private ICouTeachingJobExecuteService couTeachingJobExecuteService;
    @Autowired
    private ICouClassService couClassService;
    @Autowired
    private ICouCourScheTemplateAtomService couCourScheTemplateAtomService;
    @Autowired
    private ISysInfo sysInfo;

    /**
     * 分页列表查询
     *
     * @param couCourseArrange
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "cou_course_arrange-分页列表查询")
    @ApiOperation(value = "cou_course_arrange-分页列表查询", notes = "cou_course_arrange-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(CouCourseArrange couCourseArrange,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        Map<String,String> queryParam = sysInfo.getMapQueryParamByReq(req);
        //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值克隆给vo
        BaseApiEntity baseApiEntity = sysInfo.querySysInfoCloneObj(req,couCourseArrange,false); //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值给vo
        couCourseArrange.setCreateUser(null);
//        couCourseArrange.setCreateBy(null);
        couCourseArrange.setCreateTime(null);
        couCourseArrange.setEditUser(null);
        couCourseArrange.setEditTime(null);
//        couCourseArrange.setUpdateBy(null);
//        couCourseArrange.setUpdateTime(null);
        QueryWrapper<CouCourseArrange> queryWrapper = QueryGenerator.initQueryWrapper(couCourseArrange, req.getParameterMap());
        Page<CouCourseArrange> page = new Page<CouCourseArrange>(pageNo, pageSize);
        IPage<CouCourseArrange> pageList = couCourseArrangeService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 分页列表查询 listStatus
     *
     * @param couCourseArrange
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "cou_course_arrange-分页列表查询")
    @ApiOperation(value = "cou_course_arrange-分页列表查询", notes = "cou_course_arrange-分页列表查询")
    @GetMapping(value = "/listStatus")
    public Result<?> listStatus(CouCourseArrange couCourseArrange,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        Map<String,String> queryParam = sysInfo.getMapQueryParamByReq(req);
        //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值克隆给vo
        BaseApiEntity baseApiEntity = sysInfo.querySysInfoCloneObj(req,couCourseArrange,false); //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值给vo
        couCourseArrange.setCreateUser(null);
//        couCourseArrange.setCreateBy(null);
        couCourseArrange.setCreateTime(null);
        couCourseArrange.setEditUser(null);
        couCourseArrange.setEditTime(null);
//        couCourseArrange.setUpdateBy(null);
//        couCourseArrange.setUpdateTime(null);
        //忽略项
        if(oConvertUtils.isNotEmpty(queryParam.get("ignorePropertyList"))){
            if(queryParam.get("ignorePropertyList").indexOf("1") != -1 ){
                couCourseArrange.setSchoolYearId(null); //忽略学年
            }
            if(queryParam.get("ignorePropertyList").indexOf("2") != -1 ){
                couCourseArrange.setSemesterId(null);//忽略学期
            }
//            if(queryParam.get("ignorePropertyList").indexOf("3") != -1 ){
//                couCourseArrange.setSysOrgCode(null);//忽略部门
//                couCourseArrange.setSubDepSysOrgCodes(null);
//                couCourseArrange.setSubDepIdsList(null);
//            }
//            if(queryParam.get("ignorePropertyList").indexOf("4") != -1 ){
//                couCourseArrange.setClassGradesessionYear(null);//忽略届级
//            }

        }
//        // includePropertyList 包含属性：查询当前用户的课表（老师/学生/班级等）
//        if(oConvertUtils.isNotEmpty(queryParam.get("includePropertyList")) && queryParam.get("includePropertyList").indexOf("1") != -1 ){
//            reSetObjectValueByUsersClass(baseApiEntity,couCourseArrangeAtomAllPro);
//        }


        QueryWrapper<CouCourseArrange> queryWrapper = QueryGenerator.initQueryWrapper(couCourseArrange, req.getParameterMap());
        Page<CouCourseArrange> page = new Page<CouCourseArrange>(pageNo, pageSize);
        List<String> status = new ArrayList<>();
        //课表状态应该在查询条件中设置默认值和查询范围（例如允许查询 3 4 6状态的数据）
//        status.add("3"); //已排课
//        status.add("4"); //已发布
//        status.add("6"); //重排
//        queryWrapper.in("course_arrange_status",status);
        queryWrapper.eq("tenant_id", TenantContext.getTenant());
        //只选了年级，没选班级-->按年级查询所有班级
        if(oConvertUtils.isNotEmpty(queryParam.get("gradeId")) && oConvertUtils.isEmpty(couCourseArrange.getTargetObjId())){
            QueryWrapper<CouClass> queryWrapperCouClass = new QueryWrapper<CouClass>();
            queryWrapperCouClass.in("grade_code",queryParam.get("gradeId").split(","));
            List<CouClass> classLists = couClassService.list(queryWrapperCouClass);
            List<String> classIds = new ArrayList<String>();
            if(oConvertUtils.listIsNotEmpty(classIds)) {
                for (CouClass classone : classLists) {
                    classIds.add(classone.getId());
                }
                queryWrapper.in("target_obj_id", classIds);
            }
        }

        //按年级查询条件查询：使用in查询
        List<CouClass> couClassLists =  couClassService.queryByGradeId(couCourseArrange.getGradeId());
        if (oConvertUtils.listIsNotEmpty(couClassLists)) {
            List<String> classIdList = new ArrayList<>();
//遍历集合取值
            couClassLists.forEach(item->{
                classIdList.add(item.getId());
            });
            queryWrapper.in("target_obj_id", classIdList);
        }


        queryWrapper.orderByDesc( "school_year_id");
        queryWrapper.orderByAsc("semester_id");
        queryWrapper.orderByAsc("course_arrange_order");
        // 中文排序
//        queryWrapper.orderBy(true, true, "CONVERT(name using gbk)");


        IPage<CouCourseArrange> pageList = couCourseArrangeService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param couCourseArrange
     * @return
     */
    @AutoLog(value = "cou_course_arrange-添加")
    @ApiOperation(value = "cou_course_arrange-添加", notes = "cou_course_arrange-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody CouCourseArrange couCourseArrange) {
        couCourseArrangeService.save(couCourseArrange);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param couCourseArrange
     * @return
     */
    @AutoLog(value = "cou_course_arrange-编辑")
    @ApiOperation(value = "cou_course_arrange-编辑", notes = "cou_course_arrange-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody CouCourseArrange couCourseArrange) {
        System.out.println(couCourseArrange);
        couCourseArrangeService.updateById(couCourseArrange);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "cou_course_arrange-通过id删除")
    @ApiOperation(value = "cou_course_arrange-通过id删除", notes = "cou_course_arrange-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
//        couCourseArrangeService.removeById(id);
        LambdaUpdateWrapper<CouCourseArrange> updateWrapper = new UpdateWrapper<CouCourseArrange>()
                .lambda()
                .in(CouCourseArrange::getId,Arrays.asList(id.split(",")))
                .set(CouCourseArrange::getIzDelete,1);
        couCourseArrangeService.update(updateWrapper);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "cou_course_arrange-批量删除")
    @ApiOperation(value = "cou_course_arrange-批量删除", notes = "cou_course_arrange-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
//        this.couCourseArrangeService.removeByIds(Arrays.asList(ids.split(",")));
        LambdaUpdateWrapper<CouCourseArrange> updateWrapper = new UpdateWrapper<CouCourseArrange>()
                .lambda()
                .in(CouCourseArrange::getId,Arrays.asList(ids.split(",")))
                .set(CouCourseArrange::getIzDelete,1);
        couCourseArrangeService.update(updateWrapper);
        return Result.OK("批量删除成功!");
    }
    /**
     * 批量停用
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "cou_course_arrange-批量停用")
    @ApiOperation(value = "cou_course_arrange-批量停用", notes = "cou_course_arrange-批量停用")
    @DeleteMapping(value = "/izOpen0Batch")
    public Result<?> izOpen0Batch(@RequestParam(name = "ids", required = true) String ids) {
//        this.couCourseArrangeService.removeByIds(Arrays.asList(ids.split(",")));
        LambdaUpdateWrapper<CouCourseArrange> updateWrapper = new UpdateWrapper<CouCourseArrange>()
                .lambda()
                .in(CouCourseArrange::getId,Arrays.asList(ids.split(",")))
                .set(CouCourseArrange::getIzOpen,0);
        couCourseArrangeService.update(updateWrapper);
        return Result.OK("批量停用成功!");
    }
    /**
     * 批量启用
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "cou_course_arrange-批量启用")
    @ApiOperation(value = "cou_course_arrange-批量启用", notes = "cou_course_arrange-批量启用")
    @DeleteMapping(value = "/izOpen1Batch")
    public Result<?> izOpen1Batch(@RequestParam(name = "ids", required = true) String ids) {
//        this.couCourseArrangeService.removeByIds(Arrays.asList(ids.split(",")));
        LambdaUpdateWrapper<CouCourseArrange> updateWrapper = new UpdateWrapper<CouCourseArrange>()
                .lambda()
                .in(CouCourseArrange::getId,Arrays.asList(ids.split(",")))
                .set(CouCourseArrange::getIzOpen,1);
        couCourseArrangeService.update(updateWrapper);
        return Result.OK("批量启用成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "cou_course_arrange-通过id查询")
    @ApiOperation(value = "cou_course_arrange-通过id查询", notes = "cou_course_arrange-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        CouCourseArrange couCourseArrange = couCourseArrangeService.getById(id);
        if (couCourseArrange == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(couCourseArrange);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param couCourseArrange
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CouCourseArrange couCourseArrange) {
        return super.exportXls(request, couCourseArrange, CouCourseArrange.class, "cou_course_arrange");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CouCourseArrange.class);
    }


    /**
     * 课表状态 批量修改
     * editBatch
     */
    @AutoLog(value = "cou_course_arrange-批量修改")
    @ApiOperation(value = "cou_course_arrange-批量修改", notes = "cou_course_arrange-批量修改")
    @PutMapping(value = "/editBatch")
    public Result<?> editBatch(@RequestBody JSONObject jsonObject,HttpServletRequest req) {
        String ids = jsonObject.getString("ids");//批量多选的ids
        String courseReleaseIndex = jsonObject.getString("courseReleaseIndex");//课表状态courseReleaseIndex
        //发布状态
        List<CouCourseArrange> couCourseArrangeList = new ArrayList<>();
        List<String> couCourseArrangeIds = new ArrayList<>();
        //批量多个id
        String[] array = ids.split(",");
        for (String id : array) {
            //id 单个id，set至对象中
            CouCourseArrange couCourseArrange = new CouCourseArrange();
            couCourseArrange.setId(id);//id
            couCourseArrange.setCourseArrangeStatus(courseReleaseIndex);//课表状态-传过来
            //放存值
            couCourseArrangeList.add(couCourseArrange);
            couCourseArrangeIds.add(id);
        }
        if("4".equals(courseReleaseIndex)){
            BaseApiEntity baseApiEntity = sysInfo.getBaseApiEntity(req);
            CouTeachingJobExecuteVo couTeachingJobExecuteVo = new CouTeachingJobExecuteVo();
            couTeachingJobExecuteVo.setCourseArrangeIdArray(couCourseArrangeIds.toArray(new String[couCourseArrangeIds.size()]));
            couTeachingJobExecuteVo.setArrangeStatus(courseReleaseIndex);
            return  couTeachingJobExecuteService.runTeachingJobExecute(couTeachingJobExecuteVo,baseApiEntity);
        }
        couCourseArrangeService.updateBatchById(couCourseArrangeList);
        return Result.OK("修改完成！");
    }

    /**
     * 课表状态 撤回、下线
     * editBatch
     */
    @AutoLog(value = "cou_course_arrange-批量修改")
    @ApiOperation(value = "cou_course_arrange-批量修改", notes = "cou_course_arrange-批量修改")
    @PutMapping(value = "/editBatchOldStop")
    public Result<?> editBatchOldStop(@RequestBody JSONObject jsonObject,HttpServletRequest req) {
        String id = jsonObject.getString("id");//批量多选的ids
        String courseReleaseIndex = jsonObject.getString("courseReleaseIndex");//课表状态courseReleaseIndex
        //发布状态
        CouCourseArrange couCourseArrange =new CouCourseArrange();
        //撤回重排
        if("6".equals(courseReleaseIndex)){
            couCourseArrange.setId(id);
            CouCourseArrange couCourseArr = couCourseArrangeService.getById(id);
            if("4".equals(couCourseArr.getCourseArrangeStatus())){
                couCourseArrange.setCourseArrangeStatus(courseReleaseIndex);
                couCourseArrangeService.updateById(couCourseArrange);
            }else {
                return Result.OK("选择的数据不是已发布状态的，请直接修改。不需要撤回操作！");
            }

        }
        //下线的时候 需要将3，6 状态的已排课，已撤回重排的数据进行下线，并修改执行计划表数据为失效
        if("5".equals(courseReleaseIndex)){
            BaseApiEntity baseApiEntity = sysInfo.getBaseApiEntity(req);
            CouTeachingJobExecuteVo couTeachingJobExecuteVo = new CouTeachingJobExecuteVo();
            couTeachingJobExecuteVo.setCourseArrangeIdArray(new String[]{id});
            couTeachingJobExecuteVo.setArrangeStatus(courseReleaseIndex);
            return  couTeachingJobExecuteService.runTeachingJobExecute(couTeachingJobExecuteVo,baseApiEntity);
        }
        return Result.OK("修改完成！");

    }



    /**
     * 复制课表 copyCourseArrange
     */
    @AutoLog(value = "cou_course_arrange-复制课表")
    @ApiOperation(value = "cou_course_arrange-复制课表", notes = "cou_course_arrange-复制课表")
    @PostMapping(value = "/copyCourseArrange")
    public Result<?> copyCourseArrange(@RequestBody JSONObject jsonObject,HttpServletRequest req) {

        //组装查询参数
        //组装vo以外的其他参数
        Map<String,String> queryParam = sysInfo.getMapQueryParamByReq(req);
        //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值克隆给vo
        BaseApiEntity baseApiEntity = sysInfo.getBaseApiEntity(req); //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值给vo


        //复制课表，自定义赋值(//id，//课表名称,//课表学期//课表学年//课表班级//班级教室//课表启用开始时间//课表启用结束时间)
        JSONObject objArrangeAtom = jsonObject.getJSONObject("couCourseArrangeAtom");//前端自定义赋值参数(其中id为旧课表id，其余为新生成课表的一些字段值)
        String oldId = objArrangeAtom.getString("id");//课表ID
        //通过id查询要复制的课表数据
        CouCourseArrange couCourseArrange = couCourseArrangeService.getById(oldId);
        //准备set新课标的特定字段值
        couCourseArrange.setId(null);
        couCourseArrange.setCourseArrangeCode(couCourseArrange.getCourseArrangeCode());//课表编码
        couCourseArrange.setCourseArrangeName(objArrangeAtom.getString("courseArrangeName"));//名称
        couCourseArrange.setTargetObjId(objArrangeAtom.getString("targetObjId"));//班级
        couCourseArrange.setSemesterId(objArrangeAtom.getString("semesterId"));//学期
        couCourseArrange.setSchoolYearId(objArrangeAtom.getString("schoolYearId"));//学期
        couCourseArrange.setOpenStartTime(objArrangeAtom.getDate("openStartTime"));//课表启用开始时间
        couCourseArrange.setOpenStopTime(objArrangeAtom.getDate("openStopTime"));//课表启用结束时间
        couCourseArrange.setCreateUser(baseApiEntity.getLoginUser().getUsername());//创建人
        couCourseArrange.setCourscheSourceId(oldId);//课表来源（模板id或来源课表id）
        couCourseArrange.setCreateTime(null);//清空创建时间
        couCourseArrange.setEditTime(null);//清空修改时间
        couCourseArrange.setEditUser(null);//清空修改人
        couCourseArrangeService.save(couCourseArrange);//保存新课标数据（save之后，实体已经包含最新的id）
        //完成了课表的上级复制后，开始查询及保存课表子级数据
        //教室-如果前端复制时不指定教室，则赋值null,后台不进行教室新值插入操作
        CouCourseArrangeAtom couCourseArrangeAtom = new CouCourseArrangeAtom();
        couCourseArrangeAtom.setCourseArrangeId(oldId);//通过旧课表上级id查询它的子级数据
        List<CouCourseArrangeAtom> arrangeAtomListOld = couCourseArrangeAtomService.list(new QueryWrapper<CouCourseArrangeAtom>(couCourseArrangeAtom));
        for (CouCourseArrangeAtom courseArrangeAtomObj : arrangeAtomListOld) {
            //对旧值做更新
            courseArrangeAtomObj.setId(null);
            courseArrangeAtomObj.setCourseArrangeId(couCourseArrange.getId());//新课标id
            courseArrangeAtomObj.setTargetObjId(objArrangeAtom.getString("targetObjId"));//目标对象id(班级id(默认)
            //教室-如果前端指定了教室（即classroom不为空），则判断类型为2（关联对象类型(1:班级;2:教室,3.老师;4.科目5学生)）的进行赋新值
            if (oConvertUtils.isNotEmpty(objArrangeAtom.getString("classroomId"))){
                //排除课间操、大课间等课程（courseArrangeAtomObj.getRelObjId().length() > 3）
                if (courseArrangeAtomObj.getRelObjTtype() == 2 && courseArrangeAtomObj.getRelObjId().length() > 3){
                    courseArrangeAtomObj.setRelObjId(objArrangeAtom.getString("classroomId"));
                }
            }
            //课表明细（元素）的参数处理完后，存数据
//            couCourseArrangeAtomService.save(courseArrangeAtomObj);
        }
        //课表明细（元素）的参数处理完后，存数据:批量保存
        couCourseArrangeAtomService.saveBatch(arrangeAtomListOld);
        return Result.OK("课表复制成功！");
    }

    /**
     * 初始化课表 initCourseArrangeAtom
     */
    @AutoLog(value = "cou_course_arrange-初始化课表")
    @ApiOperation(value = "cou_course_arrange-初始化课表", notes = "cou_course_arrange-初始化课表")
    @PostMapping(value = "/initCourseArrangeAtom")
    public Result<?> initCourseArrangeAtom(@RequestBody Object requestParm,HttpServletRequest req) {
        String resultmsg = "";
        //@RequestBody Object requestParm,
        Map requestParmMap = JSONObject.parseObject(JSONObject.toJSONString(requestParm), Map.class);
//        StuStudentVo stuStudentVo = new StuStudentVo();
//        BeanUtils.copyProperties(requestParm,stuStudentVo);//拷贝属性,map类型无法复制
        //此map给vo赋值的函数：可以考虑map转vo，或json转vo
//        StuStudentVo stuStudentVo = MapBeanUtils.map2Bean(StuStudentVo.class,requestParmMap);

        //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值克隆给vo
//            BaseApiEntity baseApiEntity = sysInfo.querySysInfoCloneObj(req,stuStudentVo,false); //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值给vo

//		 String requestParmStr = JSONObject.toJSONString(requestParm);
//        Map requestParmMap = JSONObject.parseObject(JSONObject.toJSONString(requestParm), Map.class);

        String ids = requestParmMap.get("ids").toString();
        LambdaQueryWrapper<CouCourseArrange> couCourseArrangeQueryWrapper = new LambdaQueryWrapper<CouCourseArrange>();
        couCourseArrangeQueryWrapper.in(CouCourseArrange::getId,ids.split(","));
        List<CouCourseArrange> couCourseArrangeList = couCourseArrangeService.list(couCourseArrangeQueryWrapper);
        for( CouCourseArrange ca :couCourseArrangeList){
            //检查明细是否存在，如果明细存在则不会初始化（跳过）
            LambdaQueryWrapper<CouCourseArrangeAtom> couCourseArrangeAtomQueryWrapper = new LambdaQueryWrapper<CouCourseArrangeAtom>();
            couCourseArrangeAtomQueryWrapper.eq(CouCourseArrangeAtom::getCourseArrangeId,ca.getId());
            List<CouCourseArrangeAtom> couCourseArrangeAtomList1 = couCourseArrangeAtomService.list(couCourseArrangeAtomQueryWrapper);
            //判断是否已存在，存在则忽略 存在需删除重建则先删除
            if(oConvertUtils.listIsNotEmpty(couCourseArrangeAtomList1)){
                resultmsg += "["+ca.getCourseArrangeName()+"]课表已存在无需初始化";
                continue;
            }
            if(oConvertUtils.isNotEmpty(ca.getCourscheTempId())) {
                //按照课表配置的模板初始化课表
                List<CouCourseArrangeAtom> newCouCourseArrangeAtomList = couCourseArrangeAtomService.getNewCouCourseArrangeAtomByCouCourseArrange(ca);
                //判断是否得到数据，进行批量保存
                if (oConvertUtils.listIsNotEmpty(newCouCourseArrangeAtomList)) {
                    couCourseArrangeAtomService.saveBatch(newCouCourseArrangeAtomList);
                    resultmsg += "["+ca.getCourseArrangeName()+"]已初始化！";
                    ca.setCourseArrangeStatus("1");//更改状态为初始化状态
                    couCourseArrangeService.updateById(ca);
                }
                else{
                    resultmsg += "["+ca.getCourseArrangeName()+"]课表模板有误，无法初始化";
                }
            }
            else{
                resultmsg += "["+ca.getCourseArrangeName()+"]课表未配置模板无法初始化";
            }
        }
        return Result.OK(resultmsg);
    }

    /**
     * 删除后重新初始化课表 delInitCourseArrangeAtom
     */
    @AutoLog(value = "cou_course_arrange-删除后重新初始化课表")
    @ApiOperation(value = "cou_course_arrange-删除后重新初始化课表", notes = "cou_course_arrange-删除后重新初始化课表")
    @PostMapping(value = "/delInitCourseArrangeAtom")
    public Result<?> delInitCourseArrangeAtom(@RequestBody Object requestParm,HttpServletRequest req) {
        String resultmsg = "";
        //@RequestBody Object requestParm,
        Map requestParmMap = JSONObject.parseObject(JSONObject.toJSONString(requestParm), Map.class);
//        StuStudentVo stuStudentVo = new StuStudentVo();
//        BeanUtils.copyProperties(requestParm,stuStudentVo);//拷贝属性,map类型无法复制
        //此map给vo赋值的函数：可以考虑map转vo，或json转vo
//        StuStudentVo stuStudentVo = MapBeanUtils.map2Bean(StuStudentVo.class,requestParmMap);

        //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值克隆给vo
//            BaseApiEntity baseApiEntity = sysInfo.querySysInfoCloneObj(req,stuStudentVo,false); //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值给vo

//		 String requestParmStr = JSONObject.toJSONString(requestParm);
//        Map requestParmMap = JSONObject.parseObject(JSONObject.toJSONString(requestParm), Map.class);

        String ids = requestParmMap.get("ids").toString();
        LambdaQueryWrapper<CouCourseArrange> couCourseArrangeQueryWrapper = new LambdaQueryWrapper<CouCourseArrange>();
        couCourseArrangeQueryWrapper.in(CouCourseArrange::getId,ids.split(","));
        List<CouCourseArrange> couCourseArrangeList = couCourseArrangeService.list(couCourseArrangeQueryWrapper);
        for( CouCourseArrange ca :couCourseArrangeList){
            //检查明细是否存在，如果明细存在则删除后重新初始化（删除明细）
            LambdaQueryWrapper<CouCourseArrangeAtom> couCourseArrangeAtomQueryWrapper = new LambdaQueryWrapper<CouCourseArrangeAtom>();
            couCourseArrangeAtomQueryWrapper.eq(CouCourseArrangeAtom::getCourseArrangeId,ca.getId());
            List<CouCourseArrangeAtom> couCourseArrangeAtomList1 = couCourseArrangeAtomService.list(couCourseArrangeAtomQueryWrapper);
            //判断是否已存在，存在则忽略 存在需删除重建则先删除
            if(oConvertUtils.listIsNotEmpty(couCourseArrangeAtomList1)){
                couCourseArrangeAtomService.removeBatchByIds(couCourseArrangeAtomList1);
                resultmsg += "["+ca.getCourseArrangeName()+"]课表已清除明细数据然后重新初始化";

            }
            if(oConvertUtils.isNotEmpty(ca.getCourscheTempId())) {
                //按照课表配置的模板初始化课表
                List<CouCourseArrangeAtom> newCouCourseArrangeAtomList = couCourseArrangeAtomService.getNewCouCourseArrangeAtomByCouCourseArrange(ca);
                //判断是否得到数据，进行批量保存
                if (oConvertUtils.listIsNotEmpty(newCouCourseArrangeAtomList)) {
                    couCourseArrangeAtomService.saveBatch(newCouCourseArrangeAtomList);
                    resultmsg += "["+ca.getCourseArrangeName()+"]已初始化！";
                    ca.setCourseArrangeStatus("1");//更改状态为初始化状态
                    couCourseArrangeService.updateById(ca);
                }
                else{
                    resultmsg += "["+ca.getCourseArrangeName()+"]课表模板有误，无法初始化";
                }
            }
            else{
                resultmsg += "["+ca.getCourseArrangeName()+"]课表未配置模板无法初始化";
            }
        }
        return Result.OK(resultmsg);
    }
    /**
     * 刷新课表的老师 refashArrangeAtomTeacher
     */
    @AutoLog(value = "cou_course_arrange-刷新课表的老师")
    @ApiOperation(value = "cou_course_arrange-刷新课表的老师", notes = "cou_course_arrange-刷新课表的老师")
    @PostMapping(value = "/refashArrangeAtomTeacher")
    public Result<?> refashArrangeAtomTeacher(@RequestBody Object requestParm,HttpServletRequest req) {
        String resultmsg = "";
        //@RequestBody Object requestParm,
        Map requestParmMap = JSONObject.parseObject(JSONObject.toJSONString(requestParm), Map.class);
        String ids = requestParmMap.get("ids").toString();
        LambdaQueryWrapper<CouCourseArrange> couCourseArrangeQueryWrapper = new LambdaQueryWrapper<CouCourseArrange>();
        couCourseArrangeQueryWrapper.in(CouCourseArrange::getId, ids.split(","));
        List<CouCourseArrange> couCourseArrangeList = couCourseArrangeService.list(couCourseArrangeQueryWrapper);
        for (CouCourseArrange ca : couCourseArrangeList) {

            if(oConvertUtils.isNotEmpty(ca.getId()) && oConvertUtils.isNotEmpty(ca.getTargetObjId())) {
                //按照课表对应班级的科目--查找对应的老师，对错误的老师找到正确的老师，然后批量保存
                List<CouCourseArrangeAtom> newCouCourseArrangeAtomList = couCourseArrangeAtomService.getNewTeacherByCouCourseArrange(ca);
                //判断是否得到数据，进行批量保存
                if (oConvertUtils.listIsNotEmpty(newCouCourseArrangeAtomList)) {
                    couCourseArrangeAtomService.updateBatchById(newCouCourseArrangeAtomList);
                    resultmsg += "["+ca.getCourseArrangeName()+"]已按分配的老师更新为最新分配的老师！";
                    couCourseArrangeService.updateById(ca);
                }
                else{
                    resultmsg += "["+ca.getCourseArrangeName()+"]已是最新老师无需更新";
                }
            }
            else{
                resultmsg += "["+ca.getCourseArrangeName()+"]课表数据有误，无法更新老师";
            }
        }
        return Result.OK(resultmsg);
    }
    /**
     * 刷新课表的教室 refashArrangeAtomClassroom
     */
    @AutoLog(value = "cou_course_arrange-刷新课表的教室")
    @ApiOperation(value = "cou_course_arrange-刷新课表的教室", notes = "cou_course_arrange-刷新课表的教室")
    @PostMapping(value = "/refashArrangeAtomClassroom")
    public Result<?> refashArrangeAtomClassroom(@RequestBody Object requestParm,HttpServletRequest req) {
        String resultmsg = "";
        //@RequestBody Object requestParm,
        Map requestParmMap = JSONObject.parseObject(JSONObject.toJSONString(requestParm), Map.class);
        String ids = requestParmMap.get("ids").toString();
        LambdaQueryWrapper<CouCourseArrange> couCourseArrangeQueryWrapper = new LambdaQueryWrapper<CouCourseArrange>();
        couCourseArrangeQueryWrapper.in(CouCourseArrange::getId, ids.split(","));
        List<CouCourseArrange> couCourseArrangeList = couCourseArrangeService.list(couCourseArrangeQueryWrapper);
        for (CouCourseArrange ca : couCourseArrangeList) {
            if(oConvertUtils.isNotEmpty(ca.getId()) && oConvertUtils.isNotEmpty(ca.getTargetObjId())) {
                //按照课表对应班级的科目--查找对应的老师，对错误的老师找到正确的老师，然后批量保存
                List<CouCourseArrangeAtom> newCouCourseArrangeAtomList = couCourseArrangeAtomService.getNewClassroomByCouCourseArrange(ca);
                //判断是否得到数据，进行批量保存
                if (oConvertUtils.listIsNotEmpty(newCouCourseArrangeAtomList)) {
                    couCourseArrangeAtomService.updateBatchById(newCouCourseArrangeAtomList);
                    resultmsg += "["+ca.getCourseArrangeName()+"]已按分配的教室更新为最新分配的教室！";
                    couCourseArrangeService.updateById(ca);
                }
                else{
                    resultmsg += "["+ca.getCourseArrangeName()+"]已是最新教室无需更新";
                }
            }
            else{
                resultmsg += "["+ca.getCourseArrangeName()+"]课表数据有误，无法更新教室";
            }
        }
        return Result.OK(resultmsg);
    }

}
